<template>
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item label="摄像头名称" prop="name">
          <el-input
              v-model="queryParams.name"
              placeholder="请输入摄像头名称"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top: 10px;" v-loading="loading">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              v-hasPermi="['iot:monitor:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['iot:monitor:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['iot:monitor:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['iot:monitor:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-divider/>

      <el-empty description="暂无设备" v-if="monitorList.length <= 0"></el-empty>
      <el-row v-else>
        <el-col v-for="(item, index) in monitorList" :key="index"
                :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
          <div class="card">
            <div class="card-content">
              <div class="card-content-img">
                <img class="productImg" src="@/assets/images/shebei.png">
              </div>
              <div class="card-content-right">
                <div class="card-content-right-title">
                  <span>{{ item.name }}</span>
                </div>
                <div class="card-content-right-info">
                  <div class="card-content-right-info-one">
                    <div class="card-content-right-info-one-topic">ip</div>
                    <div class="card-content-right-info-one-content">{{ item.ip }}</div>
                  </div>
                </div>
              </div>
            </div>
            <el-row>
              <el-col :span="12">
                <el-button class="card-button" icon="Edit" @click="handleUpdate(item)"
                           v-hasPermi="['iot:monitor:edit']">
                  修改
                </el-button>
              </el-col>
              <el-col :span="12">
                <el-button class="card-button" icon="Delete" @click="handleDelete(item)"
                           v-hasPermi="['iot:monitor:remove']">
                  删除
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-col>
      </el-row>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改监控对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="monitorRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="摄像头名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入摄像头名称"/>
        </el-form-item>
        <el-form-item label="ip" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入ip"/>
        </el-form-item>
        <el-form-item label="后台登录用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入后台登录用户名"/>
        </el-form-item>
        <el-form-item label="后台登录密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入后台登录密码"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Monitor">
import {listMonitor, getMonitor, delMonitor, addMonitor, updateMonitor} from "@/api/iot/monitor";

const {proxy} = getCurrentInstance();

const monitorList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
  },
  rules: {
    name: [
      {required: true, message: "摄像头名称不能为空", trigger: "blur"}
    ],
    ip: [
      {required: true, message: "ip不能为空", trigger: "blur"}
    ],
    userName: [
      {required: true, message: "后台登录用户名不能为空", trigger: "blur"}
    ],
    password: [
      {required: true, message: "后台登录密码不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询监控列表 */
function getList() {
  loading.value = true;
  listMonitor(queryParams.value).then(response => {
    monitorList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    monitorId: null,
    name: null,
    ip: null,
    userName: null,
    password: null,
    rtsp: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("monitorRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.monitorId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监控";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _monitorId = row.monitorId || ids.value
  getMonitor(_monitorId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改监控";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["monitorRef"].validate(valid => {
    if (valid) {
      if (form.value.monitorId != null) {
        updateMonitor(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMonitor(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _monitorIds = row.monitorId || ids.value;
  proxy.$modal.confirm('是否确认删除监控编号为"' + _monitorIds + '"的数据项？').then(function () {
    return delMonitor(_monitorIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('iot/monitor/export', {
    ...queryParams.value
  }, `monitor_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.card {
  width: 100%;
  padding-bottom: 20px;
}

.card-content {
  box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
  display: flex;
  padding: 20px;
  width: 100%;
  border-top: 1px solid var(--el-border-color);
  border-left: 1px solid var(--el-border-color);
  border-right: 1px solid var(--el-border-color);
}

.productImg {
  width: 80px;
  height: 80px;
}

.card-content-right {
  width: 100%;
}

.card-content-right-title {
  font-weight: 600;
  font-size: 16px;
  margin-left: 20px;
  margin-top: 10px;
}

.card-content-right-info {
  margin-left: 20px;
  margin-top: 10px;
  display: flex;
}

.card-content-right-info-two {
  margin-left: 40px;
}

.card-content-right-info-one-topic {
  color: #000000b3;
  font-size: 12px;
}

.card-content-right-info-two-topic {
  color: #000000b3;
  font-size: 12px;
}

.card-button {
  border-radius: 0 !important;
  width: 100% !important;
}
</style>